package org.hyy.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.hyy.dao.AdminDAO;
import org.hyy.dao.CategoryDAO;
import org.hyy.dao.PayDAO;
import org.hyy.dao.ProductDAO;
import org.hyy.vo.Category;
import org.hyy.vo.Pay;
import org.hyy.vo.Product;

/**
 * 管理员操作
 * @author yuanyuan.han
 *
 */
public class AdminAction extends BasicAction {
	private static final long serialVersionUID = -3521388458679617652L;
	private List<Category> users;
	private List<Category> categories;
	private List<Product> products;
	private int totalRecords;
	private int totalPages;
	private List<Pay> payList;
	
	public List<Pay> getPayList() {
		return payList;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public int getTotalRecords() {
		return totalRecords;
	}

	public List<Category> getUsers() {
		return users;
	}

	public List<Category> getCategories() {
		return categories;
	}

	public List<Product> getProducts() {
		return products;
	}

	public String showAdmin(){
		return "showadmin";
	}
	
	@SuppressWarnings("unchecked")
	public String logAdmin(){
		HttpServletRequest request = getRequest();
		String name = request.getParameter("username");
		String passwd = request.getParameter("passwd");
		
		String a = "admin";
		String b = "admin";
		
		if(a.equals(name) && b.equals(passwd)){
			session.put("admin", name);
			return "logsuccess";
		}else{
			return "logerr";
		}
	}
	
	@SuppressWarnings("unchecked")
	public String menuAdmin(){
		//接收参数
		String a = getRequest().getParameter("menuid");
		String pageno = getRequest().getParameter("p");
		int menuid = Integer.parseInt(a);
		//声明变量
		AdminDAO adminDAO = null;
		CategoryDAO categoryDAO = null; 
		ProductDAO productDAO = null;
		
		//判断用户点击了哪一个菜单
		switch (menuid) {
		case 1:
			adminDAO = new AdminDAO();
			users = adminDAO.showall();
			//返回数据
			break;
		case 2:
			categoryDAO = new CategoryDAO();
			categories = new ArrayList<Category>();
			categories = categoryDAO.GetCatergories();
			break;
		case 3:
			productDAO = new ProductDAO();
			products = new ArrayList<Product>();
			if("".equals(pageno)||pageno==null){
				totalRecords = productDAO.getRecoder(products, 1, 10, false);
			}else{
				totalRecords = productDAO.getRecoder(products, Integer.valueOf(pageno), 10, false);
			}
			if(totalRecords<0){
				totalRecords = 0;
			}
			totalPages = (totalRecords + 2 - 1) / 2;
			break;
		case 4:
			payList = new ArrayList<Pay>();
			PayDAO payDAO = new PayDAO();
			payList = payDAO.showall();
			break;
		default:
			break;
		}
		if(menuid==1){
			return "alluser";
		}else if(menuid==2){
			return "allcategory";
		}else if(menuid==3){
			return "allproducts";
		}else{
			return "showpay";
		}
	}
}
